import time
import xml.etree.ElementTree as ET
from datetime import datetime

class XMLBuilder:
    @staticmethod
    def heartbeat(device_id, status="OK"):
        """构建心跳XML"""
        root = ET.Element("Notify")
        ET.SubElement(root, "CmdType").text = "Keepalive"
        ET.SubElement(root, "SN").text = str(int(time.time()))
        ET.SubElement(root, "DeviceID").text = device_id
        ET.SubElement(root, "Status").text = status
        return ET.tostring(root, encoding='utf-8').decode()
    
    @staticmethod
    def catalog_response(device_id, channels):
        """构建设备目录响应"""
        root = ET.Element("Response")
        ET.SubElement(root, "CmdType").text = "Catalog"
        ET.SubElement(root, "SN").text = str(int(time.time()))
        ET.SubElement(root, "DeviceID").text = device_id
        ET.SubElement(root, "SumNum").text = str(len(channels))
        
        device_list = ET.SubElement(root, "DeviceList")
        for channel in channels:
            item = ET.SubElement(device_list, "Item")
            ET.SubElement(item, "DeviceID").text = channel["id"]
            ET.SubElement(item, "Name").text = channel["name"]
            ET.SubElement(item, "Manufacturer").text = "Acme Inc"
            ET.SubElement(item, "Model").text = "IPCam Pro"
            ET.SubElement(item, "Status").text = channel["status"]
            ET.SubElement(item, "Parental").text = "0"
        
        return ET.tostring(root, encoding='utf-8').decode()
    
    @staticmethod
    def device_info_response(device_id, device_info):
        """构建设备信息响应"""
        root = ET.Element("Response")
        ET.SubElement(root, "CmdType").text = "DeviceInfo"
        ET.SubElement(root, "SN").text = str(int(time.time()))
        ET.SubElement(root, "DeviceID").text = device_id
        
        ET.SubElement(root, "DeviceName").text = device_info["name"]
        ET.SubElement(root, "Manufacturer").text = device_info["manufacturer"]
        ET.SubElement(root, "Model").text = device_info["model"]
        ET.SubElement(root, "Firmware").text = device_info["firmware"]
        ET.SubElement(root, "Channel").text = str(len(device_info["channels"]))
        
        return ET.tostring(root, encoding='utf-8').decode()
    
    @staticmethod
    def playback_response(device_id, records):
        """构建回放记录响应"""
        root = ET.Element("Response")
        ET.SubElement(root, "CmdType").text = "RecordInfo"
        ET.SubElement(root, "SN").text = str(int(time.time()))
        ET.SubElement(root, "DeviceID").text = device_id
        ET.SubElement(root, "Name").text = "RecordList"
        ET.SubElement(root, "SumNum").text = str(len(records))
        
        record_list = ET.SubElement(root, "RecordList")
        for record in records:
            item = ET.SubElement(record_list, "Item")
            ET.SubElement(item, "DeviceID").text = device_id
            ET.SubElement(item, "Name").text = record["name"]
            ET.SubElement(item, "FilePath").text = record["path"]
            ET.SubElement(item, "Address").text = record["address"]
            ET.SubElement(item, "StartTime").text = record["start"]
            ET.SubElement(item, "EndTime").text = record["end"]
            ET.SubElement(item, "Secrecy").text = "0"
            ET.SubElement(item, "Type").text = "time"
        
        return ET.tostring(root, encoding='utf-8').decode()
    
    @staticmethod
    def ptz_response(device_id, result_code):
        """PTZ控制响应"""
        root = ET.Element("Response")
        ET.SubElement(root, "CmdType").text = "DeviceControl"
        ET.SubElement(root, "SN").text = str(int(time.time()))
        ET.SubElement(root, "DeviceID").text = device_id
        ET.SubElement(root, "Result").text = result_code
        return ET.tostring(root, encoding='utf-8').decode()
    
    @staticmethod
    def alarm_notify(device_id, alarm_type, description):
        """报警通知"""
        root = ET.Element("Notify")
        ET.SubElement(root, "CmdType").text = "Alarm"
        ET.SubElement(root, "SN").text = str(int(time.time()))
        ET.SubElement(root, "DeviceID").text = device_id
        
        info = ET.SubElement(root, "Info")
        ET.SubElement(info, "AlarmPriority").text = "1"
        ET.SubElement(info, "AlarmMethod").text = "1"
        ET.SubElement(info, "AlarmTime").text = datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
        ET.SubElement(info, "AlarmDescription").text = description
        ET.SubElement(info, "Longitude").text = "0.0"
        ET.SubElement(info, "Latitude").text = "0.0"
        
        items = ET.SubElement(info, "Info")
        item = ET.SubElement(items, "Item")
        ET.SubElement(item, "AlarmType").text = str(alarm_type)
        
        return ET.tostring(root, encoding='utf-8').decode()